package com.nsoft.thickclient.trackMove.account;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.Box;
import javax.swing.JPanel;

import com.nsoft.server.service.main.AccountService;
import com.nsoft.server.service.main.PartyService;
import com.nsoft.server.serviceImpl.FacadeFactoryImpl;
import com.nsoft.thickclient.beanUtils.ActionDescriptor;
import com.nsoft.thickclient.beanUtils.NButton;
import com.nsoft.thickclient.controller.ScreenController;
import com.nsoft.thickclient.trackMove.intr.account.Account;
import com.nsoft.thickclient.trackMove.intr.party.Party;

public class AccountViewController extends ScreenController {

	private AccountView accountView;

	private JPanel mainPanel;

	private boolean editMode;

	private ActionDescriptor saveAction = new ActionDescriptor("Save",
			"doSave", this);

	private ActionDescriptor newAction = new ActionDescriptor("New", "doNew",
			this);

	private ActionDescriptor cancelAction = new ActionDescriptor("Cancel",
			"doCancel", this);

	public AccountViewController() {
		this.editMode = false;
	}

	public AccountViewController(boolean isEditMode) {
		this.editMode = isEditMode;
	}

	@Override
	public void close() {
		// TODO Auto-generated method stub

	}

	@Override
	public String getScreenTitle() {
		return "Mange account";
	}

	@Override
	public JPanel getView() {
		return mainPanel;
	}

	@Override
	public void init() {
		mainPanel = new JPanel(new BorderLayout());
		accountView = new AccountView();
		mainPanel.add(accountView, BorderLayout.PAGE_START);
		mainPanel.add(getButtonPanel(), BorderLayout.PAGE_END);

	}

	private JPanel getButtonPanel() {
		JPanel butonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		butonPanel.add(new NButton(saveAction));
		butonPanel.add(Box.createHorizontalStrut(20));
		butonPanel.add(new NButton(newAction, !editMode));
		butonPanel.add(Box.createHorizontalStrut(20));
		butonPanel.add(new NButton(cancelAction));
		return butonPanel;
	}

	public void doSave() {
		accountView.getModel();
		AccountService service = FacadeFactoryImpl.getAccountService();
		Account party =service.saveAccount((Account)accountView.getModel().getValueObject());
		if (!editMode) {
			accountView.setWorkingBean(party);
			doNew();
		} else {
			doCancel();

		}
	}

	public void doNew() {
		accountView.getModel().setValueObject(new Account());
	}

	public void doCancel() {
		getApplicationController().closeScreen(this);
	}

	public AccountView getAccountView() {
		return accountView;
	}

	public void setAccountView(AccountView accountView) {
		this.accountView = accountView;
	}

	public boolean isEditMode() {
		return editMode;
	}

	public void setEditMode(boolean isEditMode) {
		this.editMode = isEditMode;
	}
}
